﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoNiuOA.Models;

namespace TaoNiuOA.Web.Controllers
{
    public class WorkdayController : Controller
    {
        private DbEntity db = new DbEntity();

        [IsLogon]
        public ViewResult Index()
        {
            var month = DateTime.Now.ToString("yyyy-MM");
            var list = new List<Workday>();

            var mod = Request.QueryString["mod"];
            ViewData["mod"] = mod;

            //根据条件查询
            if (!string.IsNullOrEmpty(mod)&&mod.IndexOf("-") != -1)
            {
                var num=Convert.ToInt32(mod.Replace("-",""));
                if (num < 201111)
                {
                    Response.Write("<script>alert('只能查询2011-11以后的数据.');</script>");
                    return View(list);
                }
                //限制5个月内的工作时间设置
                var five=Convert.ToInt32(DateTime.Now.AddMonths(5).ToString("yyyyMM"));
                if(num>five)
                {
                    Response.Write("<script>alert('只能设置5个月内的数据.');</script>");
                    return View(list);
                }
                list = (from n in db.Workdays where n.Date.Contains(mod) select n).ToList();
                if (list.Count() == 0)
                {
                    Initialition(mod);
                    list = (from n in db.Workdays where n.Date.Contains(mod) select n).ToList();
                }
            }
            else
            {
                switch (mod)
                {
                    //下个月工作时间
                    case "nextmonth":
                        month = DateTime.Now.AddMonths(1).ToString("yyyy-MM");
                        list = (from n in db.Workdays where n.Date.Contains(month) select n).ToList();
                        if (list.Count() == 0)
                        {
                            Initialition(month);
                            list = (from n in db.Workdays where n.Date.Contains(month) select n).ToList();
                        }
                        break;
                    //上个月工作时间
                    case "lastmonth":
                        month = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
                        list = (from n in db.Workdays where n.Date.Contains(month) select n).ToList();
                        if (list.Count() == 0)
                            Response.Write("<script>alert('没有您要的数据.');</script>");
                        break;
                    //默认,本月
                    default:
                        list = (from n in db.Workdays where n.Date.Contains(month) select n).ToList();
                        if (list.Count() == 0)
                        {
                            Initialition(month);
                            list = (from n in db.Workdays where n.Date.Contains(month) select n).ToList();
                        }
                        break;
                }
            }

            return View(list);
        }

        /// <summary>
        /// 根据一周工作生成时间,初始化每个月的工作日历
        /// </summary>
        /// <param name="yearMonth">年月.形式必须如:yyyy-MM</param>
        public void Initialition(string yearMonth)
        {
            try
            {
                var year = Convert.ToInt16(yearMonth.Split('-')[0]);
                var month = Convert.ToInt16(yearMonth.Split('-')[1]);
                var days = DateTime.DaysInMonth(year, month);
                for (var i = 1; i <= days; i++)
                {
                    Workday wd = new Workday();
                    if (i < 10)
                        wd.Date = yearMonth + "-" +"0" + i;
                    else
                        wd.Date = yearMonth + "-" + i;

                    var dayOfWeek = ShenLan.Common.DateTimeHandler.GetWeekdayByDate(wd.Date);
                    var workTime = db.WorkTimes.SingleOrDefault(n => n.DayOfWeek == dayOfWeek);
                    wd.DayOfWeek = dayOfWeek;
                    wd.OnWork = workTime.OnWork;
                    wd.OffWork = workTime.OffWork;
                    wd.IsHoliday = false;
                    if (dayOfWeek == "星期天")
                        wd.IsHoliday = true;
                    db.Workdays.Add(wd);
                    db.SaveChanges();
                }
            }
            catch { }
        }

        [IsLogon]
        public ActionResult Edit(int id)
        {
            Workday workday = db.Workdays.Find(id);
            return View(workday);
        }

        [IsLogon]
        [HttpPost]
        public ActionResult Edit(Workday workday)
        {
            if (ModelState.IsValid)
            {
                db.Entry(workday).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(workday);
        }

        //public ViewResult Details(int id)
        //{
        //    Workday workday = db.Workdays.Find(id);
        //    return View(workday);
        //}


        //public ActionResult Delete(int id)
        //{
        //    Workday workday = db.Workdays.Find(id);
        //    return View(workday);
        //}

        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(int id)
        //{
        //    Workday workday = db.Workdays.Find(id);
        //    db.Workdays.Remove(workday);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}